package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.subject.OneSubjectVo;
import com.atguigu.eduservice.entity.subject.TwoSubjectVo;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-04-11
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //课程分类列表
    @Override
    public List<OneSubjectVo> getAllEduSubject() {
        //1 查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        //parent_id=0
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //2 查询所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        //parent_id!=0
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);

        //创建返回最终结果集合
        List<OneSubjectVo> finalSubjectList = new ArrayList<>();

        //3 封装一级分类
        //List<EduSubject> oneSubjectList  ==> List<OneSubjectVo> finalSubjectList
        // 遍历查询出来所有一级分类集合
        for (int i = 0; i < oneSubjectList.size(); i++) {
            //得到每个一级分类对象
            EduSubject oneSubject = oneSubjectList.get(i);
            //eduSubject对象值，设置到OneSubjectVo对象里面，把OneSubjectVo对象放到finalSubjectList里面
            OneSubjectVo oneSubjectVo = new OneSubjectVo();
            //传统写法
//            oneSubjectVo.setTitle(eduSubject.getTitle());
//            oneSubjectVo.setId(eduSubject.getId());
//            finalSubjectList.add(oneSubjectVo);
            //上面代码简化
            BeanUtils.copyProperties(oneSubject,oneSubjectVo);
            finalSubjectList.add(oneSubjectVo);

            //创建集合，用于封装二级分类
            List<TwoSubjectVo> twoSubjectVos = new ArrayList<>();
            //遍历所有的二级分类
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //得到每个二级分类
                EduSubject twoSubject = twoSubjectList.get(m);
                //判断一级分类id和二级分类parentid是否相同
                if(oneSubject.getId().equals(twoSubject.getParentId())) {
                    //进行封装
                    // twoSubject对象值 复制 到TwoSubjectVo里面
                    TwoSubjectVo twoSubjectVo = new TwoSubjectVo();
                    BeanUtils.copyProperties(twoSubject,twoSubjectVo);
                    //放到二级分类集合里面
                    twoSubjectVos.add(twoSubjectVo);
                }
            }
            //最后一步，把获取到一级里面所有二级分类集合，放到一级 分类对象里面
            oneSubjectVo.setChildren(twoSubjectVos);
        }
        return finalSubjectList;
    }
}
